package com.hongyi.demo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.hongyi.common.excel.core.ExcelResult;
import com.hongyi.common.excel.utils.ExcelUtil;
import com.hongyi.demo.domain.Tomas;
import com.hongyi.demo.domain.TomasWrite;
import com.hongyi.demo.service.ItomasService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TomasServiceImpl implements ItomasService {


    @Override
    public Boolean readData(MultipartFile file, HttpServletResponse response) throws IOException {
        ExcelResult<Tomas> tomasExcelResult = ExcelUtil.importExcel(file.getInputStream(), Tomas.class, false);
        List<Tomas> list = tomasExcelResult.getList();
        Map<String, Tomas> tomasMap = new HashMap<>();
        for (Tomas tomas : list) {
            String cellName = tomas.getCellName();
            String area = cellName.substring(0, 4);
            if (tomasMap.containsKey(area)) {
                Tomas tomasData = tomasMap.get(area);
                tomasData.setRrcRequest(tomas.getRrcRequest() + tomasData.getRrcRequest());
                tomasData.setRrcSuccess(tomas.getRrcSuccess() + tomasData.getRrcSuccess());
                tomasData.setERabRequest(tomas.getERabRequest() + tomasData.getERabRequest());
                tomasData.setERabSuccess(tomas.getERabSuccess() + tomasData.getERabSuccess());

            } else {
                tomasMap.put(area, tomas);
            }
        }
        // 将Map转换回List
        List<Tomas> dataList = new ArrayList<>(tomasMap.values());
        List<TomasWrite> tomasWriteList = new ArrayList<>();
        for (Tomas tomas : dataList) {
            Long rrcSuccess = tomas.getRrcSuccess();
            Long rrcRequest = tomas.getRrcRequest();
            Long eRabRequest = tomas.getERabRequest();
            Long erabSuccess = tomas.getERabSuccess();

            BigDecimal callingRate = BigDecimal.ZERO;
            if (rrcRequest != 0 && eRabRequest != 0) {
                BigDecimal rrcRatio = BigDecimal.valueOf(rrcSuccess).divide(BigDecimal.valueOf(rrcRequest), 10, RoundingMode.HALF_UP);
                BigDecimal erabRatio = BigDecimal.valueOf(erabSuccess).divide(BigDecimal.valueOf(eRabRequest), 10, RoundingMode.HALF_UP);
                callingRate = rrcRatio.multiply(erabRatio);
            }

            // 将比率转换为百分比字符串
            String callingRateStr = callingRate.multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP).toString() + "%";

            TomasWrite tomasWrite = new TomasWrite();
            tomasWrite.setArea(tomas.getCellName().substring(0, 4));
            tomasWrite.setCallingRate(callingRateStr);
            tomasWriteList.add(tomasWrite);
        }
        String fileName = "e:/ceshi1.xlsx";
        //ExcelUtil.exportExcel(tomasWriteList,"接通率",TomasWrite.class,response);
        EasyExcel.write(fileName, TomasWrite.class)
                .sheet("地市接通率")
                .doWrite(tomasWriteList);
        return true;
    }
}
